﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KCL.Code.Data
{
    public class MaterialImportDAO : BaseDAO
    {
        public MaterialImport GetMaterialImportByID(int id)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var materialImport = (from mimp in ctx.MaterialImports
                                      where mimp.id == id
                                      select mimp).FirstOrDefault();
                return materialImport;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<MaterialImport> GetAllMaterialImports()
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var lstMaterialImports = from mimp in ctx.MaterialImports
                                         orderby mimp.date descending
                                         select mimp;
                return lstMaterialImports.AsEnumerable().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool Insert(MaterialImport mi)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                ctx.MaterialImports.InsertOnSubmit(mi);
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool Update(MaterialImport mi)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var materialImportToUpdate = (from mimp in ctx.MaterialImports
                                              where mimp.id == mi.id
                                              select mimp).FirstOrDefault();
                materialImportToUpdate.materialCode = mi.materialCode;
                materialImportToUpdate.quantity = mi.quantity;
                materialImportToUpdate.date = mi.date;
                materialImportToUpdate.importer = mi.importer;
                materialImportToUpdate.note = mi.note;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(MaterialImport mi)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                ctx.MaterialImports.DeleteOnSubmit(ctx.MaterialImports.Where(
                    mimp => mimp.id == mi.id).FirstOrDefault());
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public int GetMaxID()
        {
            KCLDataContext ctx = new KCLDataContext();
            int? maxid = (from mimp in ctx.MaterialImports
                          select (int?)mimp.id).Max();
            return maxid == null ? -1 : (int)maxid;
        }
    }
}